package com.example.wangandroid.ui.login.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.example.wangandroid.R
import com.example.wangandroid.adapter.TreeAdapter
import com.example.wangandroid.adapter.TreeAdapterList
import com.example.wangandroid.data.model.ItemViewModel
import com.example.wangandroid.databinding.FragmentItemListBinding

/**
 * A fragment representing a list of Items.
 */
class TreeFragment : Fragment() {

    private var columnCount = 1
    private lateinit var itemModel : ItemViewModel
    private  var treeAdapter: TreeAdapter ? =null
    private lateinit var recyclerView:RecyclerView
    private var _binding: FragmentItemListBinding? = null
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            columnCount = it.getInt(ARG_COLUMN_COUNT)
        }
        itemModel = ItemViewModel()
        itemModel.getIteminfo()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentItemListBinding.inflate(inflater,container,false)
        val view =binding.root
        LogUtils.d("onCreateView =="+view)
        // android.widget.FrameLayout{96b717d V.E...... ......ID 0,0-0,0}
        return view
    }



    fun initView(view :View){
        recyclerView = view.findViewById(R.id.list)
        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false)
        itemModel.TreeClassRusult.observe(viewLifecycleOwner, Observer{
            if(treeAdapter==null){
                treeAdapter = it?.let { it1 -> TreeAdapter(it1) }
                recyclerView.adapter = treeAdapter
            }else{
                treeAdapter!!.setData(it)
                treeAdapter!!.notifyDataSetChanged()
            }
        })
    }

    override fun onStart() {
        super.onStart()
        LogUtils.d("onStart")
    }

    override fun onResume() {
        super.onResume()
        LogUtils.d("onResume")
    }

    override fun onPause() {
        super.onPause()
        LogUtils.d("onPause")
    }

    override fun onStop() {
        super.onStop()
        LogUtils.d("onStop")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        LogUtils.d("onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtils.d("onDestroy")
    }

    override fun onDetach() {
        super.onDetach()
        LogUtils.d("onDetach")
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        LogUtils.d("onViewCreated")
        initView(view)
    }

    companion object {

        // TODO: Customize parameter argument names
        const val ARG_COLUMN_COUNT = "column-count"

        // TODO: Customize parameter initialization
        @JvmStatic
        fun newInstance(columnCount: Int) =
            TreeFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_COLUMN_COUNT, columnCount)
                }
            }
    }
}